/* Copyright (c) 2017 FIRST. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted (subject to the limitations in the disclaimer below) provided that
 * the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice, this list
 * of conditions and the following disclaimer.
 *
 * Redistributions in binary form must reproduce the above copyright notice, this
 * list of conditions and the following disclaimer in the documentation and/or
 * other materials provided with the distribution.
 *
 * Neither the name of FIRST nor the names of its contributors may be used to endorse or
 * promote products derived from this software without specific prior written permission.
 *
 * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS
 * LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package org.firstinspires.ftc.teamcode;

import com.qualcomm.robotcore.eventloop.opmode.Disabled;
import com.qualcomm.robotcore.eventloop.opmode.LinearOpMode;
import com.qualcomm.robotcore.eventloop.opmode.TeleOp;
import com.qualcomm.robotcore.hardware.DcMotor;
import com.qualcomm.robotcore.hardware.DcMotorEx;
import com.qualcomm.robotcore.hardware.Servo;
import com.qualcomm.robotcore.util.ElapsedTime;
import com.qualcomm.robotcore.util.Range;

import org.checkerframework.checker.units.qual.A;
import org.firstinspires.ftc.robotcore.external.navigation.AngleUnit;


/**
 * This file contains an minimal example of a Linear "OpMode". An OpMode is a 'program' that runs in either
 * the autonomous or the teleop period of an FTC match. The names of OpModes appear on the menu
 * of the FTC Driver Station. When an selection is made from the menu, the corresponding OpMode
 * class is instantiated on the Robot Controller and executed.
 *
 * This particular OpMode just executes a basic Tank Drive Teleop for a two wheeled robot
 * It includes all the skeletal structure that all linear OpModes contain.
 *
 * Use Android Studios to Copy this Class, and Paste it into your team's code folder with a new name.
 * Remove or comment out the @Disabled line to add this opmode to the Driver Station OpMode list
 */

@TeleOp( group="Linear Opmode")

public class Manual19602 extends LinearOpMode {

    // Declare OpMode members.
    private ElapsedTime runtime = new ElapsedTime();
    private DcMotorEx lfDrive = null;
    private DcMotorEx lbDrive = null;
    private DcMotorEx rfDrive = null;
    private DcMotorEx rbDrive = null;
    private DcMotorEx armDrive = null;
    private DcMotorEx spinDrive = null;
    private DcMotorEx liftDrive = null;
    private DcMotorEx intakeDrive = null;
    private Servo armServo = null;
    private Servo clawServo = null;
    private Servo boxServo = null;
    private int[] liftLevel = {0,2000,3000,4000};//-----
    int liftIndex = 0;//-----


    @Override
    public void runOpMode() {
        telemetry.addData("Status", "Initialized");
        telemetry.update();

        // Initialize the hardware variables. Note that the strings used here as parameters
        // to 'get' must correspond to the names assigned during the robot configuration
        // step (using the FTC Robot Controller app on the phone).
        lfDrive  = hardwareMap.get(DcMotorEx.class, "LF");
        lbDrive= hardwareMap.get(DcMotorEx.class, "LB");
        rfDrive= hardwareMap.get(DcMotorEx.class, "RF");
        rbDrive= hardwareMap.get(DcMotorEx.class, "RB");
        armDrive= hardwareMap.get(DcMotorEx.class, "AR");
        spinDrive= hardwareMap.get(DcMotorEx.class, "SP");
        liftDrive= hardwareMap.get(DcMotorEx.class, "LI");
        intakeDrive= hardwareMap.get(DcMotorEx.class, "IN");

        liftDrive.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
        liftDrive.setZeroPowerBehavior(DcMotor.ZeroPowerBehavior.BRAKE);
        liftDrive.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);//-----
        liftDrive.setMode(DcMotor.RunMode.RUN_TO_POSITION);//-----
        liftDrive.setVelocity(3*360,AngleUnit.DEGREES);//-----
        armDrive.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
        armDrive.setZeroPowerBehavior(DcMotor.ZeroPowerBehavior.BRAKE);

        armServo = hardwareMap.get(Servo.class,"David");
        clawServo = hardwareMap.get(Servo.class,"Hudson");
        boxServo = hardwareMap.get(Servo.class,"Lucas");

        // Most robots need the motor on one side to be reversed to drive forward
        // Reverse the motor that runs backwards when connected directly to the battery
        rfDrive.setDirection(DcMotor.Direction.REVERSE);
        rbDrive.setDirection(DcMotor.Direction.REVERSE);

        // Wait for the game to start (driver presses PLAY)
        waitForStart();
        runtime.reset();

        // run until the end of the match (driver presses STOP)
        while (opModeIsActive()) {
            double speedRate = 1;
            if(gamepad1.left_stick_button || gamepad1.right_stick_button)
                speedRate = 0.3;
            if(Math.abs(gamepad1.left_stick_y)>0.1){
                lfDrive.setPower(-gamepad1.left_stick_y*speedRate);
                lbDrive.setPower(-gamepad1.left_stick_y*speedRate);
                rfDrive.setPower(-gamepad1.left_stick_y*speedRate);
                rbDrive.setPower(-gamepad1.left_stick_y*speedRate);
            }
            if(Math.abs(gamepad1.right_stick_x)>0.1){
                lfDrive.setPower(gamepad1.left_stick_y*speedRate);
                lbDrive.setPower(gamepad1.left_stick_y*speedRate);
                rfDrive.setPower(-gamepad1.left_stick_y*speedRate);
                rbDrive.setPower(-gamepad1.left_stick_y*speedRate);
            }
            if(gamepad1.dpad_left){
                lfDrive.setPower(-1);
                lbDrive.setPower(1);
                rfDrive.setPower(1);
                rbDrive.setPower(-1);
            }
            if(gamepad1.dpad_right){
                lfDrive.setPower(1);
                lbDrive.setPower(-1);
                rfDrive.setPower(-1);
                rbDrive.setPower(1);
            }
            //控制转盘
            if(gamepad1.left_bumper){
                spinDrive.setVelocity(4*360, AngleUnit.DEGREES);
            }
            //控制机械臂
            if(Math.abs(gamepad1.left_stick_y)>0.1){
                armDrive.setPower(gamepad2.left_stick_y);
                if(gamepad2.left_stick_y<0){
                    armServo.setPosition(0);
                }
                if(gamepad2.left_stick_y>0){
                    armServo.setPosition(1);
                }
            }
            //控制升降
            if(Math.abs(gamepad1.left_trigger)>0){
                liftDrive.setVelocity(3*360,AngleUnit.DEGREES);
            }else if(Math.abs(gamepad1.right_trigger)>0){
                liftDrive.setVelocity(-3*360,AngleUnit.DEGREES);
            }else {
                liftDrive.setVelocity(0);
            }
            ////-----
            if(Math.abs(gamepad1.left_trigger)>0){
                liftIndex++;
                liftIndex=Math.min(2,liftIndex);

            }else if(Math.abs(gamepad1.right_trigger)>0){
                liftIndex--;
                liftIndex=Math.max(0,liftIndex);
            }
            liftDrive.setTargetPosition(liftLevel[liftIndex]);
            //-----

            //手指的开合控制
            if(gamepad2.right_stick_x>0){
                clawServo.setPosition(1);
            }
            if(gamepad2.right_stick_x<0){
                clawServo.setPosition(0);
            }
            if(gamepad1.a){
                boxServo.setPosition(1);
            }
            if(gamepad1.b){
                boxServo.setPosition(0);
            }
            // Show the elapsed game time and wheel power.
            telemetry.addData("Status", "Run Time: " + runtime.toString());
            telemetry.addData("armServo", "(%.2f))", armServo.getPosition());
            telemetry.addData("clawServo", "(%.2f))", clawServo.getPosition());
            telemetry.addData("boxServo", "(%.2f))", boxServo.getPosition());
            telemetry.addData("liftDrive", "(%.2f))", liftDrive.getVelocity());
            telemetry.update();
        }
    }
}
